package com.cssw.fyzb.wap.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.wap.model.vo.ImgVo;
import com.cssw.fyzb.wap.model.vo.WxOperIdVo;
import com.cssw.fyzb.wap.util.FileUtil;
import com.ydn.appserver.MinaAppClient;
import com.ydn.appserver.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: zzf
 * @time: 2020/12/23 16:51
 */
@Service
public class ResourcesService extends BaseService {

    @Autowired
    private MinaAppClient tpsClient;

    @Autowired
    private MinaAppClient busiClient;


    /**
     * 图片上传
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [multipartFile]
     **/
    public AjaxPage<ImgVo> imgFile(MultipartFile[] multipartFiles) throws Exception {
        List list = new ArrayList();
        for (MultipartFile multipartFile : multipartFiles) {
            Request request = new Request("AddRes");
            request.setParameter("type", "image");
            File file = FileUtil.multipartFileToFile(multipartFile);
            //传输后缀名
            request.setParameter("extension", multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1));
            request.setParameter("content", FileUtil.getBase64(file));
            Object data = tpsClient.send(request).getData();
            file.delete();
            list.add(data);
        }

        return AjaxPage.success(list, list.size(), list.size());
    }

    /**
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [userId, liveId, koUserId]
     **/
    public AjaxData proportion(String paramCode) {
        Request request = new Request("Proportion");
        request.setParameter("paramCode", paramCode);
        return convertData(busiClient.send(request));
    }

    /**
     * 处理视频信息
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [fileId]
     **/
    public AjaxData processMedia(String fileId) {
        Request request = new Request("ProcessMedia");
        request.setParameter("fileId", fileId);
        return convertData(tpsClient.send(request));
    }

    /**
     * 获取上传签名
     *
     * @return com.cssw.fyzb.base.web.domain.AjaxData
     * @Param [fileId]
     **/
    public AjaxData getUploadSig() {
        Request request = new Request("GetUploadSig");
        return convertData(tpsClient.send(request));
    }

    /**
     * 获取小程序operId
     *
     * @return
     * @Param
     **/
    public AjaxData getOperId(String code) {
        Request request = new Request("GetWxOpenId");
        request.setParameter("code", code);
        return convertData(tpsClient.send(request), WxOperIdVo.class);
    }
}
